<template>
  <div class="app-container">
    <t-form
        v-show="showSearch"
        ref="queryRef"
        :model="queryParams"
        class="mb20"
        layout="inline"
    >
      <t-form-item label="商品名称" prop="name">
        <t-input
            v-model="queryParams.name"
            clearable
            placeholder="请输入名称"
            style="width: 200px"
            @enter="handleQuery"
        />
      </t-form-item>
      <t-form-item>
        <t-space>
          <t-button theme="primary" @click="handleQuery">
            搜索
            <template #icon>
              <t-icon name="search"/>
            </template>
          </t-button>
          <t-button theme="default" @click="resetQuery">
            重置
            <template #icon>
              <t-icon name="refresh"/>
            </template>
          </t-button>
        </t-space>
      </t-form-item>
    </t-form>

    <t-row :gutter="10" class="mb8">
      <t-col :span="1.5">
        <t-button
            theme="warning"
            @click="handleExport"
        >导出
          <template #icon>
            <t-icon name="download"/>
          </template>
        </t-button>
      </t-col>
      <right-toolbar
          v-model:showSearch="showSearch"
          @queryTable="getList"
      ></right-toolbar>
    </t-row>

    <t-table
        v-loading="loading"
        :columns="columns"
        :data="list"
        row-key="id"
        stripe
    >

      <template #status="scope">
        <t-select v-model="scope.row.status" placeholder="更新状态" style="width: 90px">
          <t-option v-for="option in options" :key="option.value" :label="option.label" :value="option.value"
                    @click="handleStatusChange(scope.row,option)"/>
        </t-select>
      </template>

      <template #user="scope">
        <div>
          <div>{{ scope.row.nickName }}</div>
          <div>{{ scope.row.phone }}</div>
        </div>
      </template>

      <template #action="scope">
        <t-button
            :disabled="scope.row.status !== 0"
            theme="primary"
            variant="text"
            @click="handleUpdate(scope.row)"
        >修改
          <template #icon>
            <t-icon name="edit"/>
          </template>
        </t-button>
        <t-button
            :disabled="scope.row.status > 1"
            theme="primary"
            variant="text"
            @click="handleDelete(scope.row)"
        >删除
          <template #icon>
            <t-icon name="delete"/>
          </template>
        </t-button>
      </template>
    </t-table>

    <pagination
        v-show="total > 0"
        v-model:limit="queryParams.pageSize"
        v-model:page="queryParams.pageNum"
        :total="total"
        @pagination="getList"
    />
  </div>

  <!-- 添加或修改公告对话框 -->
  <t-dialog v-model:visible="open" :header="title" attach="body" width="780px">
    <t-form ref="festRef" :data="form" :rules="rules" label-width="80px">
      <t-form-item requiredMark label="商品名称" name="name" required>
        <t-input
            v-model="form.name"
            placeholder="请输入名称"
        />
      </t-form-item>
      <t-form-item requiredMark label="快递公司" name="express" required>
        <t-input
            v-model="form.express"
            placeholder="请输入快递公司"
        />
      </t-form-item>
      <t-form-item requiredMark label="单号" name="number" required>
        <t-input
            v-model="form.number"
            placeholder="请输入单号"
        />
      </t-form-item>
      <t-form-item requiredMark label="备注" name="remark" required>
        <t-input
            v-model="form.remark"
            placeholder="请输入备注"
        />
      </t-form-item>
    </t-form>
    <template #footer>
      <div class="dialog-footer">
        <t-button theme="primary" type="submit" @click="submitForm">确 定</t-button>
        <t-button theme="default" @click="cancel">取 消</t-button>
      </div>
    </template>
  </t-dialog>

</template>


<script setup>
import {
  listManifest, delManifest, updateManifest, getManifest
} from "@/api/hy/manifest";
import {getCurrentInstance, reactive, ref, toRefs} from "vue";

const {proxy} = getCurrentInstance();
const list = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const open = ref(false);
const title = ref("");

const options = [
  {label: '待收货', value: 0},
  {label: '待验收', value: 1},
  {label: '待结算', value: 2},
  {label: '已结算', value: 3},
]

const columns = ref([
  {colKey: "name", title: "商品名称",},
  {colKey: "express", title: "快递公司",},
  {colKey: "number", title: "订单号", width: 400},
  {colKey: "user", title: "用户信息",},
  {colKey: "status", title: "状态",},
  {colKey: "remark", title: "备注",},
  {colKey: "createAt", title: "货单提交时间"},
  {colKey: "action", title: "操作", width: 200,},
]);

const data = reactive({
  form: {},
  categoryList: [],
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
  },
  rules: {
    name: [
      {required: true, message: "请输入商品名称", trigger: "blur"},
      {min: 2, max: 50, message: "长度在 2 到 50 个字符", trigger: "blur"},
    ],
    express: [
      {required: true, message: "请输入快递公司", trigger: "blur"},
      {min: 2, max: 50, message: "长度在 2 到 50 个字符", trigger: "blur"},
    ],
    number: [
      {required: true, message: "请输入单号", trigger: "blur"},
      {min: 2, max: 50, message: "长度在 2 到 50 个字符", trigger: "blur"},
    ]
  }
});


const {queryParams, form} = toRefs(data);

/** 查询公告列表 */
function getList() {
  loading.value = true;
  listManifest(queryParams.value).then((response) => {
    list.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
      "hy/manifest/export",
      {
        ...queryParams.value,
      },
      `用户货单${new Date().getTime()}.xlsx`
  );
}

function handleStatusChange(row, option) {
  updateManifest({
    id: row.id,
    status: option.value,
  }).then((response) => {
    proxy.$modal.msgSuccess("货单状态更新成功");
    getList();
  })
}


/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const id = row.id;
  getManifest(id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改货单";
  });
}


/** 提交按钮 */
function submitForm() {
  proxy.$refs["festRef"].validate().then((valid) => {
    console.log('valid', valid);
    if (valid === true) {
      if (form.value.id !== undefined) {
        updateManifest(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}


/** 表单重置 */
function reset() {
  form.value = {
    id: undefined,
    name: undefined,
    express: undefined,
    number: undefined,
    remark: undefined,
  };
  proxy.resetForm("goodsRef");
}


/** 删除按钮操作 */
function handleDelete(row) {
  const id = row.id;
  proxy.$modal
      .confirm('是否确认删除编号为"' + id + '"的数据项？')
      .then(function () {
        return delManifest(id);
      })
      .then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
      })
      .catch(() => {
      });
}


getList();
</script>
